﻿var currReserve;

function getCurrReserve() {
	return currReserve;
}

$(document).ready(function () {
	drawReservesGrid();
});

function drawReservesGrid() {
	var $gridMyReserves = $("#gridMyReserves").kendoGrid({
		dataSource: {
			type: "aspnetmvc-ajax",
			transport: {
				read: {
					url: "Customer/getReserveList"
				}
			},
			error: function (e) {
				document.write(e.xhr.responseText);
			},
			schema: {
				model: {
					fields: {
						Reserve: { type: "object" }
					}
				},
				data: "Data",
				total: "Total",
				errors: "Errors"
			},
			serverPaging: true,
			serverSorting: true,
			serverGrouping: true,
			serverFiltering: true
		},
		groupable: {
			messages: {
				empty: "Arrastre una columna para agrupar."
			}
		},
		height: 450,
		sortable: true,
		selectable: true,
		resizable: true,
		reorderable: true,
		filterable: true,
		pageable: {
			pageSize: 50,
			pageSizes: [10, 50, 100]
		},
		columns: [
			{ field: "Date", title: "Fecha" },
            { field: "Time", title: "Hora" },
            { field: "Restaurant.Name", title: "Restaurante" },
            { field: "Restaurant.Address", title: "Direccion" },
            { field: "Restaurant.Telephone", title: "Telefono" },
            { field: "Restaurant.TableList[0].Description", title: "Mesa" },
			{ command: { text: "Cancelar", click: cancelReserve }, title: " ", width: "140px" }
		]
	}).data("kendoGrid");

	$gridMyReserves.element.delegate("tbody>tr", "dblclick", function () {
		currReserve = $gridMyReserves.dataItem($gridMyReserves.select());

		OpenRateRestaurantWindow();
	});
}

function OpenRateRestaurantWindow() {
	var window = document.createElement('div');
	$(window).attr('id', 'rateRestaurantWindow');

	$("#rateRestaurantWindowContainer").append(window);

	var $rateRestaurantWindow = $("#rateRestaurantWindow").kendoWindow({
		content: "Customer/getPartials?partial=RateRestaurantWindow",
		width: 500,
		modal: true,
		title: 'Calificar Restaurante',
		visible: false,
		deactivate: function () {
			this.destroy();

			$("#gridMyReserves").data("kendoGrid").dataSource.read();
		}
	}).data("kendoWindow");

	$rateRestaurantWindow.open();
}

function cancelReserve(e) {
	var answer = window.confirm("Desea cancelar la reserva?");

	if (answer == true) {
		e.preventDefault();

		var dataItem = this.dataItem($(e.currentTarget).closest("tr"));

		deleteReserve(dataItem.ID);

		$("#gridMyReserves").data("kendoGrid").dataSource.read();
	}
}